package com.zr.Paydivide.service.impl;

import com.zr.Paydivide.mapper.PaydivideMapper;
import com.zr.Paydivide.pojo.PayDivideAddVo;
import com.zr.Paydivide.pojo.Paydivide;
import com.zr.Paydivide.pojo.PaydivideShowVo;
import com.zr.Paydivide.pojo.UserRepay;
import com.zr.Paydivide.service.PaydivideService;
import com.zr.meiju.ClassEnmu;
import com.zr.meiju.CycEnum;
import com.zr.meiju.OpenBankEnum;
import com.zr.meiju.StatusEnumRepay;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * Created by star on 2019/7/3.
 */
@Service
public class PaydivideServiceImpl implements PaydivideService {

    @Autowired
    private PaydivideMapper paydivideMapper;

    @Override
    public ResultVo<PaydivideShowVo> findByLoginId(Integer loginId) {
        if (loginId==null) {
            return ResultBuildVo.error("注册信息表ID不能为空","500");
        }
        PaydivideShowVo payDivideShowVo = paydivideMapper.findByLoginId(loginId);
        String name1= OpenBankEnum.getName(payDivideShowVo.getOpeningBank());
        String name=payDivideShowVo.getBankCardAccount().substring(payDivideShowVo.getBankCardAccount().length()-4,payDivideShowVo.getBankCardAccount().length());
        payDivideShowVo.setPaymentAccount("中软云际（北京）");
        payDivideShowVo = getStartTime(payDivideShowVo, payDivideShowVo);
        payDivideShowVo.setFirstPayMentDate(payDivideShowVo.getClassHours().getMonth()+"月"+payDivideShowVo.getClassHours().getDay()+"日");
        payDivideShowVo.setPayMentDate("每月"+(payDivideShowVo.getClassHours().getDay()+1)+"日");
        payDivideShowVo.setPaymentCycleName(CycEnum.getName(payDivideShowVo.getPaymentCycle()));
        payDivideShowVo.setBackBank(name1+"("+name+")");
        payDivideShowVo.setAgreement("雪竹分期个人信息授权函");
        payDivideShowVo.setMyCourseName(ClassEnmu.getName(payDivideShowVo.getMyCourse()));
        return ResultBuildVo.success(payDivideShowVo);
    }

    @Override
    public ResultVo<PaydivideShowVo> payDivideAdd(Integer loginId) {
        if (loginId==null) {
            return ResultBuildVo.error("注册信息表ID不能为空","500");
        }
        PaydivideShowVo payDivideShowVo = paydivideMapper.findByLoginId(loginId);
        payDivideShowVo.setLoginId(loginId);
        String name1= OpenBankEnum.getName(payDivideShowVo.getOpeningBank());
        String name=payDivideShowVo.getBankCardAccount().substring(payDivideShowVo.getBankCardAccount().length()-4,payDivideShowVo.getBankCardAccount().length());
        payDivideShowVo.setPaymentAccount("中软云际（北京）");
        //PayDivideAddVo startTime = getStartTime(payDivideShowVo, payDivideAddVo);
        payDivideShowVo.setStartTime("2019-07-03--2021-06-03");
        payDivideShowVo.setFirstPayMentDate(payDivideShowVo.getClassHours().getMonth()+"月"+payDivideShowVo.getClassHours().getDay()+"日");
        payDivideShowVo.setPayMentDate("每月"+(payDivideShowVo.getClassHours().getDay()+1)+"日");
        payDivideShowVo.setPaymentCycleName(CycEnum.getName(payDivideShowVo.getPaymentCycle()));
        payDivideShowVo.setBackBank(name1+"("+name+")");
        payDivideShowVo.setAgreement("雪竹分期个人信息授权函");
        payDivideShowVo.setMyCourseName(ClassEnmu.getName(payDivideShowVo.getMyCourse()));
        PayDivideAddVo payDivideAddVo=new PayDivideAddVo();
        BeanUtils.copyProperties(payDivideShowVo,payDivideAddVo);
        payDivideAddVo.setAmountSettlement(payDivideShowVo.getAmountInstallments());
        Date nowDate = new Date();
        payDivideAddVo.setCreateTime(nowDate);
        payDivideAddVo.setCreateName("小明");
        payDivideAddVo.setUpdateTime(nowDate);
        payDivideAddVo.setUpdateName("小明");
        paydivideMapper.payDivideAdd(payDivideAddVo);
        return ResultBuildVo.success(payDivideShowVo);
    }

    private PaydivideShowVo getStartTime(PaydivideShowVo payDivideShowVo,PaydivideShowVo payDivideShowVo1) {
        Date start = new Date();
        Integer monthNum = StatusEnumRepay.getMonthNum(payDivideShowVo.getPaymentCycle());
        long startMillis = start.getTime();
        long endMillis = 1000*60*60*24*30*monthNum;
        long result = endMillis +startMillis;

        Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(result);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy/MM/dd" );
        String startStr = sdf.format(start);

        StringBuilder sb = new StringBuilder();
        sb.append(startStr).append("-")
                .append(year)
                .append("/")
                .append(month)
                .append("/")
                .append(day);
        payDivideShowVo1.setStartTime(sb.toString());
        return payDivideShowVo1;
    }


}
